<?php
defined('_IN_CMS') or die('Fuck off');

class Page extends Db
{
    public $title;     // Заголовок страницы
    public $title_url; // Имя страницы для URL
    public $php_self;
    public $section;
    public $category;
    private $username; // Имя пользователя (/user/miha)
    
    public function __construct()
    {
        $this->php_self = substr($_SERVER['PHP_SELF'], 1); // '/index.php' -> 'index.php'
        switch($this->php_self) {
            case 'add_article.php':
                $this->title = 'Добавить статью';
                $this->title_url = 'add_article';
                break;
            case 'article.php':
                $this->title = 'Статьи';
                $this->title_url = 'articles';

                if (isset($_GET['section'])) {
                    $this->section = $_GET['section'];
                    $sec_list = self::db_query("SELECT `full_title`, `title` FROM `articles_sections` 
                    WHERE `title` = '$this->section' LIMIT 1 --");
                    if (mysql_num_rows($sec_list) == 1) {
                        $this->section = ' :: <a href="/articles/'.$this->section.'">'.mysql_result($sec_list, 0, 'full_title').'</a>';
                    } else {
                        $this->section = 'yux';
                    }
                }
                if ((isset($_GET['category'])) && isset($_GET['section'])) {
                    $this->category = $_GET['category'];
                    $this->section_title = $_GET['section'];
                    $this->category_title = $_GET['category'];
                    $sec_list = self::db_query("SELECT `full_title`, `title` FROM `articles_categories` 
                    WHERE `title` = '$this->category' LIMIT 1");
                    if (mysql_num_rows($sec_list) == 1) {
                        $this->category = ' :: <a href="/articles/'.$this->section_title.'/'.$this->category_title.'">'.mysql_result($sec_list, 0, 'full_title').'</a>';
                    }
                } 
                if ((isset($_GET['category'])) && isset($_GET['section']) && isset($_GET['url_name'])) {
                    $this->url_name = $_GET['url_name'];
                    $sec_list = self::db_query("SELECT *
                    FROM `".core::$table_name['articles']."`, `articles_categories`, `articles_sections`
                    WHERE
                    `".core::$table_name['articles']."`.`section` = `articles_sections`.`id` AND 
                    `".core::$table_name['articles']."`.`category` = `articles_categories`.`id` AND 
                    `".core::$table_name['articles']."`.`url_name` = '$this->url_name'
                    LIMIT 1 --");
                    if (mysql_num_rows($sec_list) == 1) {
                        $this->topic = '<a href="/articles/'.$this->section_title.'/'.$this->category_title.'/'.$this->url_name.'">'.mysql_result($sec_list, 0, core::$table_name['articles'].'.title').'</a>';
                    }
                } 
                break;
            case 'auth.php':
                $this->title = 'Вход';
                if (isset($_SESSION['authorized'])) {
                    if ($_SESSION['authorized'] == true) {
                        $this->title = 'Панель управления';
                    }
                }
                $this->title_url = 'panel';
                break;
            case 'bookmarks.php':
                $this->title = 'Закладки';
                $this->title_url = 'bookmarks';
                break;
            case 'phorum.php':
                $this->section = '';
                $this->category = '';
                $this->title = 'Форум';
                $this->title_url = 'forum';
                if (isset($_GET['section'])) {
                    $this->section = $_GET['section'];
                    $sec_list = self::db_query("SELECT `full_title`, `title` FROM `phorum_sections` 
                    WHERE `title` = '$this->section' LIMIT 1 --");
                    if (mysql_num_rows($sec_list) == 1) {
                        $this->section = ' :: <a href="/forum/'.$this->section.'">'.mysql_result($sec_list, 0, 'full_title').'</a>';
                    }
                }
                if ((isset($_GET['section'])) && isset($_GET['category'])) {
                    $this->category = $_GET['category'];
                    $this->section_title = $_GET['section'];
                    $this->category_title = $_GET['category'];
                    $sec_list = self::db_query("SELECT `full_title`, `title` FROM `phorum_categories` 
                    WHERE `title` = '$this->category' LIMIT 1 --");
                    if (mysql_num_rows($sec_list) == 1) {
                        $this->category = ' :: <a href="/forum/'.$this->section_title.'/'.$this->category_title.'">'.mysql_result($sec_list, 0, 'full_title').'</a>';
                    }
                }
                if ((isset($_GET['section'])) && isset($_GET['category']) && isset($_GET['topic'])) {
                    $this->topic_title = $_GET['topic'];
                    $sec_list = self::db_query("SELECT `phorum_categories`.`title` AS 'cat_title', 
                    `phorum_topics`.`full_title` AS 'topic_full_title', `phorum_topics`.`title` AS 'topic_title'
                    FROM `phorum_categories`, `phorum_topics` 
                    WHERE `phorum_topics`.`title` = '$this->topic_title' 
                    LIMIT 1 --");
                    if (mysql_num_rows($sec_list) > 0) {
                        $this->topic = '<a href="/forum/'.$this->section_title.'/'.$this->category_title.'/'.$this->topic_title.'">'.mysql_result($sec_list, 0, 'topic_full_title').'</a><br>';
                    }
                }
                break;
            case 'gallery.php':
                $this->title = 'Галерея';
                $this->title_url = 'gallery';
                break;
            case 'gb.php':
                $this->title = 'Гостевая книга';
                $this->title_url = 'gb';
                break;
            case 'index.php':
                $this->title = 'Главная';
                $this->title_url = '';
                break;
            case 'logout.php':
                $this->title = 'Выход';
                $this->title_url = 'logout';
                break;
            case 'mail.php':
                $this->title = 'Сообщения';
                $this->title_url = 'mail';
                break;
            case 'registration.php':
                $this->title = 'Регистрация';
                $this->title_url = 'registration';
                break;
            case 'settings.php':
                $this->title = 'Настройки';
                $this->title_url = 'settings';
                break;
            case 'statistic.php':
                $this->title = 'Статистика';
                $this->title_url = 'statistics';
                break;
            case 'user.php':
                $this->username = (isset($_GET['name'])) ? $_GET['name'] : '';
                $this->title = 'Профиль пользователя '.$this->username;
                $this->title_url = (isset($_GET['name'])) ? 'user/'.$this->username : 'user';
                break;
            case 'userlist.php':
                $this->title = 'Пользователи';
                $this->title_url = 'userlist';
                break;
            default:
                $this->title = '0_0';
                $this->title_url = '';
                break;
        }
        if (!isset($this->topic)) {
            $this->titlel = $this->title;
            $this->title = '<div class="page_title">';
            $this->title .= '<div><a href="/'.$this->title_url.'">'.$this->titlel.'</a>'.$this->section.$this->category.'</div></div>';
        } else {
            $this->titlel = $this->title;
            $this->title = '<div class="page_title">'.$this->topic;
            $this->title .= '<div class="page_title_bot_a"><a href="/'.$this->title_url.'">'.$this->titlel.'</a>'.$this->section.$this->category.'</div></div>';
        }

    }
}

?>
